package gui;

import javax.swing.ButtonGroup;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import java.awt.Insets;
import javax.swing.JRadioButton;
import javax.swing.JButton;

import simuAnnealing.IterationsMethod;
import simuAnnealing.SAconfigurations;
import simuAnnealing.Solution;
import simuAnnealing.TempReductionMethod;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
import javax.swing.SwingConstants;
import java.awt.Color;
import java.awt.Font;

public class UserConfigurations extends JPanel {

	private JTextField alphaField;
	private JTextField betaField;
	private final ButtonGroup IterationthodGroup = new ButtonGroup();
	private JTextField goodSolutionField;
	private JTextField chosenSolField;
	private JTextField itrPerTempField;

	int numGoodSolutions = 100;
	int numChosenSolutions = 100;
	int iterationsPerTemp = 1000;
	double alpha = 0.999;
	double beta = 0.666;

	SAconfigurations config;

	private JPanel myPanel;

	private JDialog parentDialog;
	private final ButtonGroup graphOptions = new ButtonGroup();

	/**
	 * Create the panel.
	 * 
	 * @param dialog
	 * @param soluion
	 */
	public UserConfigurations(final JDialog dialog, final Solution soluion) {

		myPanel = this;
		parentDialog = dialog;
		config = soluion.getSimulatedConfigurations();

		setLayout(new BorderLayout(0, 0));

		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setFont(new Font("Tahoma", Font.PLAIN, 14));
		tabbedPane.setForeground(new Color(255, 250, 240));
		tabbedPane.setBackground(new Color(0, 0, 0));

		add(tabbedPane, BorderLayout.NORTH);

		JPanel simulatedPanel = new JPanel();
		simulatedPanel.setBackground(new Color(112, 128, 144));
		tabbedPane.addTab("Simulated Annealing settings", new ImageIcon(
				UserConfigurations.class.getResource("/Images/Run.png")),
				simulatedPanel, null);
		GridBagLayout gbl_simulatedPanel = new GridBagLayout();
		gbl_simulatedPanel.columnWidths = new int[] { 0, 0, 0, 0 };
		gbl_simulatedPanel.rowHeights = new int[] { 0, 26, 26, 23, 15, 0, 0, 0,
				0, 0, 0, 0, 0 };
		gbl_simulatedPanel.columnWeights = new double[] { 0.0, 1.0, 1.0,
				Double.MIN_VALUE };
		gbl_simulatedPanel.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
				0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
		simulatedPanel.setLayout(gbl_simulatedPanel);

		JLabel lblAlpha = new JLabel("Alpha value :");
		GridBagConstraints gbc_lblAlpha = new GridBagConstraints();
		gbc_lblAlpha.insets = new Insets(0, 0, 5, 5);
		gbc_lblAlpha.gridx = 0;
		gbc_lblAlpha.gridy = 0;
		simulatedPanel.add(lblAlpha, gbc_lblAlpha);

		alphaField = new JTextField();
		GridBagConstraints gbc_alphaField = new GridBagConstraints();
		gbc_alphaField.insets = new Insets(0, 0, 5, 5);
		gbc_alphaField.gridx = 1;
		gbc_alphaField.gridy = 0;
		simulatedPanel.add(alphaField, gbc_alphaField);
		alphaField.setColumns(10);
		alphaField.setText(String.valueOf(config.getAlfa()));

		JLabel lblBetaValue = new JLabel("Beta value :");
		GridBagConstraints gbc_lblBetaValue = new GridBagConstraints();
		gbc_lblBetaValue.insets = new Insets(0, 0, 5, 5);
		gbc_lblBetaValue.gridx = 0;
		gbc_lblBetaValue.gridy = 1;
		simulatedPanel.add(lblBetaValue, gbc_lblBetaValue);

		betaField = new JTextField();
		GridBagConstraints gbc_betaField = new GridBagConstraints();
		gbc_betaField.insets = new Insets(0, 0, 5, 5);
		gbc_betaField.gridx = 1;
		gbc_betaField.gridy = 1;
		simulatedPanel.add(betaField, gbc_betaField);
		betaField.setColumns(10);
		betaField.setText(String.valueOf(config.getBeta()));

		JLabel lblTempretureReductionWay = new JLabel("Tempreture reduction :");
		GridBagConstraints gbc_lblTempretureReductionWay = new GridBagConstraints();
		gbc_lblTempretureReductionWay.insets = new Insets(0, 0, 5, 5);
		gbc_lblTempretureReductionWay.gridx = 0;
		gbc_lblTempretureReductionWay.gridy = 2;
		simulatedPanel.add(lblTempretureReductionWay,
				gbc_lblTempretureReductionWay);

		final JRadioButton btnReduceDynamicly = new JRadioButton(
				"Reduce by dynamic factor");
		btnReduceDynamicly.setBackground(new Color(112, 128, 144));
		GridBagConstraints gbc_btnReduceDynamicly = new GridBagConstraints();
		gbc_btnReduceDynamicly.anchor = GridBagConstraints.WEST;
		gbc_btnReduceDynamicly.insets = new Insets(0, 0, 5, 5);
		gbc_btnReduceDynamicly.gridx = 1;
		gbc_btnReduceDynamicly.gridy = 2;
		simulatedPanel.add(btnReduceDynamicly, gbc_btnReduceDynamicly);

		final JRadioButton btnReduceByFactor = new JRadioButton(
				"Reduce by factor");
		btnReduceByFactor.setBackground(new Color(112, 128, 144));
		GridBagConstraints gbc_btnReduceByFactor = new GridBagConstraints();
		gbc_btnReduceByFactor.anchor = GridBagConstraints.WEST;
		gbc_btnReduceByFactor.insets = new Insets(0, 0, 5, 0);
		gbc_btnReduceByFactor.gridx = 2;
		gbc_btnReduceByFactor.gridy = 2;
		simulatedPanel.add(btnReduceByFactor, gbc_btnReduceByFactor);

		ButtonGroup tempretureGroup = new ButtonGroup();
		tempretureGroup.add(btnReduceByFactor);
		tempretureGroup.add(btnReduceDynamicly);

		JLabel lblIterationsMethod = new JLabel("Iterations method :");
		GridBagConstraints gbc_lblIterationsMethod = new GridBagConstraints();
		gbc_lblIterationsMethod.insets = new Insets(0, 0, 5, 5);
		gbc_lblIterationsMethod.gridx = 0;
		gbc_lblIterationsMethod.gridy = 4;
		simulatedPanel.add(lblIterationsMethod, gbc_lblIterationsMethod);

		final JRadioButton btnConstNumberIterations = new JRadioButton(
				"Constant number of iterations");
		btnConstNumberIterations.setBackground(new Color(112, 128, 144));
		IterationthodGroup.add(btnConstNumberIterations);
		GridBagConstraints gbc_btnConstNumberIterations = new GridBagConstraints();
		gbc_btnConstNumberIterations.anchor = GridBagConstraints.WEST;
		gbc_btnConstNumberIterations.insets = new Insets(0, 0, 5, 5);
		gbc_btnConstNumberIterations.gridx = 1;
		gbc_btnConstNumberIterations.gridy = 4;
		simulatedPanel.add(btnConstNumberIterations,
				gbc_btnConstNumberIterations);

		final JRadioButton btnNumChosenSol = new JRadioButton(
				"Number of chosen solutions");
		btnNumChosenSol.setBackground(new Color(112, 128, 144));
		IterationthodGroup.add(btnNumChosenSol);
		GridBagConstraints gbc_btnNumChosenSol = new GridBagConstraints();
		gbc_btnNumChosenSol.anchor = GridBagConstraints.WEST;
		gbc_btnNumChosenSol.insets = new Insets(0, 0, 5, 5);
		gbc_btnNumChosenSol.gridx = 1;
		gbc_btnNumChosenSol.gridy = 5;
		simulatedPanel.add(btnNumChosenSol, gbc_btnNumChosenSol);

		final JRadioButton btnNumGoodSol = new JRadioButton(
				"Number of good solutions");
		btnNumGoodSol.setBackground(new Color(112, 128, 144));
		IterationthodGroup.add(btnNumGoodSol);
		GridBagConstraints gbc_btnNumGoodSol = new GridBagConstraints();
		gbc_btnNumGoodSol.insets = new Insets(0, 0, 5, 5);
		gbc_btnNumGoodSol.anchor = GridBagConstraints.WEST;
		gbc_btnNumGoodSol.gridx = 1;
		gbc_btnNumGoodSol.gridy = 6;
		simulatedPanel.add(btnNumGoodSol, gbc_btnNumGoodSol);

		JLabel lblNewLabel = new JLabel("Good solutions:");
		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
		gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel.gridx = 0;
		gbc_lblNewLabel.gridy = 7;
		simulatedPanel.add(lblNewLabel, gbc_lblNewLabel);

		goodSolutionField = new JTextField();
		goodSolutionField.setText(String.valueOf(config
				.getNumberOfGoodSolutions()));
		GridBagConstraints gbc_goodSolutionField = new GridBagConstraints();
		gbc_goodSolutionField.insets = new Insets(0, 0, 5, 5);
		gbc_goodSolutionField.gridx = 1;
		gbc_goodSolutionField.gridy = 7;
		simulatedPanel.add(goodSolutionField, gbc_goodSolutionField);
		goodSolutionField.setColumns(10);

		JButton btnUpdateConf = new JButton("Update");
		btnUpdateConf.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				if (JOptionPane.showConfirmDialog(myPanel,
						"Are you sure you want to update SA configurations",
						"Alert!", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
					/* Updating radio button selection */
					if (btnReduceByFactor.isSelected()) {
						soluion.getSimulatedConfigurations()
								.setHowToReduceTemperature(
										TempReductionMethod.REDUCE_BY_FACTOR);
					}

					if (btnReduceDynamicly.isSelected()) {
						soluion.getSimulatedConfigurations()
								.setHowToReduceTemperature(
										TempReductionMethod.REDUCE_BY_DYNAMIC_FACTOR);
					}

					if (btnConstNumberIterations.isSelected()) {
						soluion.getSimulatedConfigurations()
								.setIterationsChoosingType(
										IterationsMethod.BY_A_CONSTANT_NUMBER);
					}

					if (btnNumChosenSol.isSelected()) {
						soluion.getSimulatedConfigurations()
								.setIterationsChoosingType(
										IterationsMethod.BY_NUMBER_OF_CHOSEN_SOLUTIONS);
					}

					if (btnNumGoodSol.isSelected()) {
						soluion.getSimulatedConfigurations()
								.setIterationsChoosingType(
										IterationsMethod.BY_NUMBER_OF_GOOD_SOLUTIONS);
					}

					/* Updating textBox fields */

					try {

						if (goodSolutionField.getText() != null
								|| (!goodSolutionField.getText().isEmpty())) {
							numGoodSolutions = Integer
									.parseInt(goodSolutionField.getText());
						}
						if (chosenSolField.getText() != null
								|| (!chosenSolField.getText().isEmpty())) {
							numChosenSolutions = Integer
									.parseInt(chosenSolField.getText());
						}
						if (itrPerTempField.getText() != null
								|| (!itrPerTempField.getText().isEmpty())) {
							iterationsPerTemp = Integer
									.parseInt(itrPerTempField.getText());
						}
						if (alphaField.getText() != null
								|| (!alphaField.getText().isEmpty())) {
							alpha = Double.parseDouble(alphaField.getText());
						}
						if (betaField.getText() != null
								|| (!betaField.getText().isEmpty())) {
							beta = Double.parseDouble(betaField.getText());
						}

						config.setNumberOfGoodSolutions(numGoodSolutions);
						config.setNumberOfChosenSolutions(numChosenSolutions);
						config.setNumberOfIterationsPerTemperature(iterationsPerTemp);
						config.setAlfa(alpha);
						config.setBeta(beta);

						JOptionPane.showMessageDialog(myPanel,
								"Configurations were updated successfully");
						parentDialog.hide();

					} catch (NumberFormatException e) {
						JOptionPane
								.showMessageDialog(
										myPanel,
										"Good solutions, Chosen solutions and Iterations per tempreture MUST be natural numbers.");
					}
				}

			}
		});
		GridBagConstraints gbc_btnUpdateConf = new GridBagConstraints();
		gbc_btnUpdateConf.insets = new Insets(0, 0, 5, 0);
		gbc_btnUpdateConf.gridx = 2;
		gbc_btnUpdateConf.gridy = 7;
		simulatedPanel.add(btnUpdateConf, gbc_btnUpdateConf);

		JLabel lblNumberOfChosen = new JLabel("Chosen solutions:");
		GridBagConstraints gbc_lblNumberOfChosen = new GridBagConstraints();
		gbc_lblNumberOfChosen.insets = new Insets(0, 0, 5, 5);
		gbc_lblNumberOfChosen.gridx = 0;
		gbc_lblNumberOfChosen.gridy = 8;
		simulatedPanel.add(lblNumberOfChosen, gbc_lblNumberOfChosen);

		chosenSolField = new JTextField();
		chosenSolField.setText(String.valueOf(config
				.getNumberOfChosenSolutions()));
		GridBagConstraints gbc_chosenSolField = new GridBagConstraints();
		gbc_chosenSolField.insets = new Insets(0, 0, 5, 5);
		gbc_chosenSolField.gridx = 1;
		gbc_chosenSolField.gridy = 8;
		simulatedPanel.add(chosenSolField, gbc_chosenSolField);
		chosenSolField.setColumns(10);

		JLabel IterationPerTempField = new JLabel("Iterations per tempreture:");
		GridBagConstraints gbc_IterationPerTempField = new GridBagConstraints();
		gbc_IterationPerTempField.anchor = GridBagConstraints.EAST;
		gbc_IterationPerTempField.insets = new Insets(0, 0, 5, 5);
		gbc_IterationPerTempField.gridx = 0;
		gbc_IterationPerTempField.gridy = 9;
		simulatedPanel.add(IterationPerTempField, gbc_IterationPerTempField);

		itrPerTempField = new JTextField();
		itrPerTempField.setText(String.valueOf(config
				.getNumberOfIterationsPerTemperature()));
		GridBagConstraints gbc_itrPerTempField = new GridBagConstraints();
		gbc_itrPerTempField.insets = new Insets(0, 0, 5, 5);
		gbc_itrPerTempField.gridx = 1;
		gbc_itrPerTempField.gridy = 9;
		simulatedPanel.add(itrPerTempField, gbc_itrPerTempField);
		itrPerTempField.setColumns(10);

		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dialog.setVisible(false);
			}
		});
		GridBagConstraints gbc_btnCancel = new GridBagConstraints();
		gbc_btnCancel.insets = new Insets(0, 0, 5, 0);
		gbc_btnCancel.gridx = 2;
		gbc_btnCancel.gridy = 9;
		simulatedPanel.add(btnCancel, gbc_btnCancel);

		JPanel panel = new JPanel();
		panel.setBackground(new Color(112, 128, 144));
		tabbedPane.addTab("Graph options", new ImageIcon(
				UserConfigurations.class.getResource("/Images/graph.png")),
				panel, null);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[] { 0, 0, 0, 0, 0 };
		gbl_panel.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
		gbl_panel.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0,
				Double.MIN_VALUE };
		gbl_panel.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
				0.0, 0.0, Double.MIN_VALUE };
		panel.setLayout(gbl_panel);

		JLabel lblPleaseChooseWhat = new JLabel(
				"Please choose what which Graph Should be plotted");
		lblPleaseChooseWhat.setFont(new Font("Tahoma", Font.PLAIN, 14));
		GridBagConstraints gbc_lblPleaseChooseWhat = new GridBagConstraints();
		gbc_lblPleaseChooseWhat.insets = new Insets(0, 0, 5, 5);
		gbc_lblPleaseChooseWhat.gridx = 2;
		gbc_lblPleaseChooseWhat.gridy = 0;
		panel.add(lblPleaseChooseWhat, gbc_lblPleaseChooseWhat);

		final JRadioButton btnByFinalSolutions = new JRadioButton(
				"Power cost as a funstion of chosen Final solutions");
		btnByFinalSolutions.setFont(new Font("Tahoma", Font.PLAIN, 13));
		btnByFinalSolutions.setBackground(new Color(112, 128, 144));
		btnByFinalSolutions.setHorizontalAlignment(SwingConstants.LEFT);
		graphOptions.add(btnByFinalSolutions);
		GridBagConstraints gbc_btnByFinalSolutions = new GridBagConstraints();
		gbc_btnByFinalSolutions.insets = new Insets(0, 0, 5, 5);
		gbc_btnByFinalSolutions.gridx = 2;
		gbc_btnByFinalSolutions.gridy = 2;
		panel.add(btnByFinalSolutions, gbc_btnByFinalSolutions);

		final JRadioButton btnShowActualSolutions = new JRadioButton(
				"Power cost as a function of chosen actual solutions");
		btnShowActualSolutions.setFont(new Font("Tahoma", Font.PLAIN, 13));
		btnShowActualSolutions.setBackground(new Color(112, 128, 144));
		btnShowActualSolutions.setHorizontalAlignment(SwingConstants.LEFT);
		graphOptions.add(btnShowActualSolutions);
		GridBagConstraints gbc_btnShowActualSolutions = new GridBagConstraints();
		gbc_btnShowActualSolutions.insets = new Insets(0, 0, 5, 5);
		gbc_btnShowActualSolutions.gridx = 2;
		gbc_btnShowActualSolutions.gridy = 5;
		panel.add(btnShowActualSolutions, gbc_btnShowActualSolutions);

		JButton btnSelect = new JButton("Select");
		btnSelect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				if ((!btnByFinalSolutions.isSelected())
						&& (!btnShowActualSolutions.isSelected())) {
					JOptionPane.showMessageDialog(myPanel,
							"You must select an option first");
				}
				if (btnByFinalSolutions.isSelected()) {
					config.setGraphMethod(GraphPlotMode.BY_FINAL_SOLUTIONS);
				}

				if (btnShowActualSolutions.isSelected()) {
					config.setGraphMethod(GraphPlotMode.BY_ACTUAL_SOLUTIONS);
				}

				JOptionPane.showMessageDialog(myPanel,
						"Configurations were updated successfully");
				parentDialog.hide();
			}
		});
		GridBagConstraints gbc_btnSelect = new GridBagConstraints();
		gbc_btnSelect.insets = new Insets(0, 0, 0, 5);
		gbc_btnSelect.gridx = 1;
		gbc_btnSelect.gridy = 7;
		panel.add(btnSelect, gbc_btnSelect);

		JButton btnNewButton = new JButton("Cancel");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dialog.setVisible(false);
			}
		});
		GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
		gbc_btnNewButton.gridx = 3;
		gbc_btnNewButton.gridy = 7;
		panel.add(btnNewButton, gbc_btnNewButton);

	}

}
